//
//  CZComposeTextView.swift
//  CZ29VVeibo
//
//  Created by apple on 16/7/3.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZComposeTextView: UITextView {
    // 重写父类已有属性
    override var font: UIFont?{
        // 通过didSet监听font改变
        didSet{
           placeholderLabel.font = font
        }
    }
    
    // 站位文字
    var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        addSubview(placeholderLabel)
        // 添加约束
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(SCREENW - 10)
        }
        // 通过系统通知监听textView 文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    // 移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    // 监听方法
    func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    
    // MARK: - 懒加载控件
    private lazy var placeholderLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.font = UIFont.systemFontOfSize(12)
        //lab.text = "我感冒了不想上课"
        lab.numberOfLines = 0
        return lab
    }()
    
    
}


